package com.vrs.controller;

import com.vrs.annotation.Idempotent;
import com.vrs.constant.RedisCacheConstant;
import com.vrs.convention.page.PageResponse;
import com.vrs.convention.result.Result;
import com.vrs.convention.result.Results;
import com.vrs.domain.dto.req.VenueDOReq;
import com.vrs.domain.dto.req.VenueListReqDTO;
import com.vrs.domain.dto.req.VenuePicDeleteReqDTO;
import com.vrs.domain.dto.req.VenuePicUploadReqDTO;
import com.vrs.domain.dto.resp.VenueRespDTO;
import com.vrs.domain.entity.VenueDO;
import com.vrs.enums.IdempotentSceneEnum;
import com.vrs.service.VenueService;
import com.vrs.service.geo.TencentGeoApi;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import com.vrs.config.MinioUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 场馆控制层
 */
@RestController
@RequestMapping("/venue/")
@RequiredArgsConstructor
@Tag(name = "场地管理")
public class VenueController {

    private final VenueService venueService;
    private final StringRedisTemplate redisTemplate;
    private final TencentGeoApi tencentGeoApi;

    @Resource
    private MinioUtil minioUtil;

    @PostMapping("/getAddressByLocation")
    @Operation(summary = "根据经纬度获取地名")
    public Result getAddressByLocation(@RequestBody VenueDOReq venueDOReq) {
        VenueDO venueDO = new VenueDO();
        BeanUtils.copyProperties(venueDOReq,venueDO);
        String address = tencentGeoApi.getAddressByLocation(venueDO.getLongitude(),venueDO.getLatitude());
        Map<String , String> resMap  = new HashMap<>();
        resMap.put("location",address);
        return Results.success(resMap);
    }

    /**
     * 通用上传请求（单个）
     */
    @Operation(summary = "图片上传")
    @PostMapping(value = "/upload")
    public Result uploadFile(MultipartFile files, HttpServletRequest request) throws Exception {
        String gatewayHost = request.getHeader("X-Gateway-Host");
        Map<String,Object> objectMap = new HashMap<>();
        objectMap.put("file",files);
        Object object = minioUtil.upload((HashMap<String, Object>) objectMap);
        return Results.success(object);
    }
    /**
     * 预约图片
     */
    @Operation(summary = "图片预览")
    @GetMapping("/preview")
    public Result uploadFile(@RequestParam("file") String file,@RequestParam("bucketName")  String bucketName) throws Exception {
        Map<String,Object> objectMap = new HashMap<>();
        objectMap.put("file",file);
        Object object = minioUtil.preview(file,bucketName);
        return Results.success(object);
    }


    /**
     * 增添数据
     */
    @PostMapping("/save")
    @Operation(summary = "新增场馆")
//    @Idempotent(
//            uniqueKeyPrefix = "vrs-venue:venue:save:",
//            key = "'_'+#venueDO.hashCode()",
//            message = "正在新增，请勿重复进行...",
//            scene = IdempotentSceneEnum.RESTAPI
//    )
    public Result save(@RequestBody VenueDOReq venueDOReq) {
        VenueDO venueDO = new VenueDO();
        BeanUtils.copyProperties(venueDOReq,venueDO);
        venueDO.setStatus(1);
        venueService.insert(venueDO);
        return Results.success();
    }

    /**
     * 查询数据
     */
    @PostMapping("/list")
    @Operation(summary = "查询场馆列表")
    @ResponseBody
    public Result<PageResponse<VenueRespDTO>> list(@RequestBody VenueListReqDTO request) {
        request.setIsCreator(0);
        PageResponse<VenueRespDTO> list = venueService.pageVenueDO(request);
        return Results.success(list);
    }
    @PostMapping("/pageVenueDOForPc")
    @Operation(summary = "查询场馆列表")
    @ResponseBody
    public Result<PageResponse<VenueRespDTO>> pageVenueDOForPc(@RequestBody VenueListReqDTO request) {
        PageResponse<VenueRespDTO> list = venueService.pageVenueDOForPc(request);
        return Results.success(list);
    }
    @PostMapping("/pageVenueDOForIsCreator")
    @Operation(summary = "查询创业者列表")
    @ResponseBody
    public Result<PageResponse<VenueRespDTO>> pageVenueDOForIsCreator(@RequestBody VenueListReqDTO request) {
        request.setIsCreator(1);
        PageResponse<VenueRespDTO> list = venueService.pageVenueDOForPc(request);
        return Results.success(list);
    }

    /**
     * 删除数据
     */
    @PostMapping("/removeById")
    @Operation(summary = "根据ID删除场馆")
    public Result removeById(@RequestBody VenueDOReq venueDOReq) {
       // venueService.validateUserType();
        VenueDO venueDO = new VenueDO();
        BeanUtils.copyProperties(venueDOReq,venueDO);
        venueService.removeById(venueDO);
        return Results.success();
    }

    /**
     * 删除数据
     */
    @PostMapping("/removeByIds")
    @Operation(summary = "根据ID集合批量删除场馆")
    public Result removeByIds( List<Long> idList) {
       // venueService.validateUserType();
        venueService.removeByIds(idList);
        return Results.success();
    }

    /**
     * 修改数据
     */
    @PostMapping("/update")
    @Operation(summary = "修改场馆")
    @ResponseBody
    public Result update(@RequestBody VenueDOReq venueDOReq) {
        VenueDO venueDO = new VenueDO();
        BeanUtils.copyProperties(venueDOReq,venueDO);

       // venueService.validateUserType();
        venueService.updateVenueDO(venueDO);

        // 删除场馆缓存
       // redisTemplate.delete(String.format(RedisCacheConstant.VENUE_GET_VENUE_BY_ID_KEY, venueDO.getId()));
        return Results.success();
    }
    /**
     * 修改数据
     */
    @PostMapping("/updateStatus")
    @Operation(summary = "修改场馆")
    @ResponseBody
    public Result updateStatus(@RequestBody VenueDOReq venueDOReq) {
        VenueDO venueDO = new VenueDO();
        BeanUtils.copyProperties(venueDOReq,venueDO);

        // venueService.validateUserType();
        venueService.updateById(venueDO);

        // 删除场馆缓存
       // redisTemplate.delete(String.format(RedisCacheConstant.VENUE_GET_VENUE_BY_ID_KEY, venueDO.getId()));
        return Results.success();
    }

    /**
     * 根据id获取场馆
     *
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")
    @Operation(summary = "根据ID获取场馆")
    public Result<VenueRespDTO> getById(@PathVariable Long id) {
        return Results.success(venueService.getVenueDespDTOById(id));
    }

    /**
     * 根据id获取场馆
     *
     * @param keyword
     * @return
     */
    @GetMapping("/listVenueType")
    @Operation(summary = "获取场馆类型")
    public Result listVenueType(@RequestParam(required = false) String keyword) {
        if (keyword == null) keyword = "";
        return Results.success(venueService.listVenueType(keyword));
    }


    /**
     * 上传图片
     *
     * @param partitionPicUploadReqDTO
     * @return
     */
    @PostMapping("/uploadPicture")
    @Operation(summary = "上传场馆图片")
    @Idempotent(
            uniqueKeyPrefix = "vrs-venue:partition:uploadPicture:",
            key = "'_'+#partitionPicUploadReqDTO.hashCode()",
            message = "正在上传，请勿重复进行...",
            scene = IdempotentSceneEnum.RESTAPI
    )
    public Result uploadPicture(@Validated @RequestBody VenuePicUploadReqDTO partitionPicUploadReqDTO) {
        venueService.savePicList(partitionPicUploadReqDTO);
        // 先修改数据库，再删除缓存
        redisTemplate.delete(String.format(RedisCacheConstant.VENUE_GET_VENUE_BY_ID_KEY, partitionPicUploadReqDTO.getVenueId()));
        return Results.success();
    }

    /**
     * 删除分区图片
     */
    @PostMapping("/removePicture")
    @Operation(summary = "删除场馆图片")
    public Result removePicture(@Validated @RequestBody VenuePicDeleteReqDTO partitionPicDeleteReqDTO) {
        venueService.removePicture(partitionPicDeleteReqDTO);
        // 先修改数据库，再删除缓存
        redisTemplate.delete(String.format(RedisCacheConstant.VENUE_GET_VENUE_BY_ID_KEY, partitionPicDeleteReqDTO.getVenueId()));
        return Results.success();
    }

}
